package com.exp.bookhouse.controller.admin;

import com.exp.bookhouse.pojo.Category;
import com.exp.bookhouse.pojo.vo.IndexVo;
import com.exp.bookhouse.pojo.vo.TableVo;
import com.exp.bookhouse.pojo.vo.chart.ChartData;
import com.exp.bookhouse.pojo.vo.chart.RowData;
import com.exp.bookhouse.service.CategoryService;
import com.exp.bookhouse.service.OrderService;
import com.exp.bookhouse.service.UserService;
import com.exp.bookhouse.service.WebSettingService;
import com.exp.bookhouse.util.HttpClientUtil;
import com.exp.bookhouse.util.NumberUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.*;

@Api(value = "首页管理接口",tags = "管理员管理首页操作")
@Controller
@RequestMapping("/admin")
public class IndexControllerAdmin {

    @Autowired
    private WebSettingService webSettingService;

    @Autowired
    private UserService userService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private CategoryService categoryService;


    @GetMapping("/toIndex")
    public String toIndex(Model model, HttpServletRequest request) throws Exception {
        //查询访问量
        Long views = webSettingService.queryWebSetting().get(0).getViews();
        //查询用户数量
        int numbers = userService.queryCountUser();
        //查询订单数量
        int orderQuantity = orderService.queryCountOrder();
        //查询交易记录
        String totalPrice = orderService.queryOrderTotalPrice();
        //获取时间
        Date date = new Date();
        //获取ip
        String ip = HttpClientUtil.getIp(request);


        IndexVo indexVo = new IndexVo();
        indexVo.setViews(views);
        indexVo.setNumbers(numbers);
        indexVo.setOrderQuantity(orderQuantity);
        indexVo.setTotalPrice(totalPrice);
        indexVo.setDate(date);
        indexVo.setIp(ip);

        model.addAttribute("indexVo",indexVo);


        return "index";
    }

    @GetMapping("/toTableYear")
    public @ResponseBody
    Map<String,Object> toTableYear() throws Exception {

        Map<String,Object> res = new HashMap<>();

        List<RowData> rows = new ArrayList<>();//chart rows

        List<Category> categories = new ArrayList<>();
        categories = categoryService.queryAllCategory();
        for(int i=0;i<categories.size();i++){
            rows.add(new RowData(categories.get(i).getName(), 0));
        }

        List<TableVo> tableVos = orderService.queryOrderByYear();//今年所有订单的书本分类的名字和书本数量
        String totalPrice = orderService.queryOrderTotalPriceByYear();//今年订单总金额
        Integer totalNum = NumberUtil.getTotalBookNum(tableVos);//今年所有书本总数量
        BigDecimal pecent = new BigDecimal("0");
        for (int i = 0; i < tableVos.size(); i++)  {
            TableVo tableVo = tableVos.get(i);
            HashMap<String, Object> map = new HashMap<>();
            String myPercent = NumberUtil.myPercent(tableVo.getBookNum(), totalNum);//百分比
            float percent = Float.parseFloat(myPercent);
            for(int j=0;j<categories.size();j++){
                if(rows.get(j).getCategory().equals(tableVo.getName())){
                    rows.get(j).setRatio(percent);
                }
            }
        }
        Collections.reverse(rows);
        ChartData chartData = new ChartData(Arrays.asList("category","ratio"),rows);
        System.out.println(chartData);
        res.put("chartData",chartData);
        res.put("totalPrice",totalPrice);
        return res;
    }


    @GetMapping("/toTableDay")
    public @ResponseBody
    Map<String,Object> toTableDay() throws Exception {

        Map<String,Object> res = new HashMap<>();

        List<RowData> rows = new ArrayList<>();//chart rows

        List<Category> categories = new ArrayList<>();
        categories = categoryService.queryAllCategory();
        for(int i=0;i<categories.size();i++){
            rows.add(new RowData(categories.get(i).getName(), 0));
        }

        List<TableVo> tableVos = orderService.queryOrderByDay();//本日所有订单的书本分类的名字和书本数量
        String totalPrice = orderService.queryOrderTotalPriceByDay();//本日订单总金额
        Integer totalNum = NumberUtil.getTotalBookNum(tableVos);//本日所有书本总数量
        BigDecimal pecent = new BigDecimal("0");
        for (int i = 0; i < tableVos.size(); i++)  {
            TableVo tableVo = tableVos.get(i);
            HashMap<String, Object> map = new HashMap<>();
            String myPercent = NumberUtil.myPercent(tableVo.getBookNum(), totalNum);//百分比
            float percent = Float.parseFloat(myPercent);
            for(int j=0;j<categories.size();j++){
                if(rows.get(j).getCategory().equals(tableVo.getName())){
                    rows.get(j).setRatio(percent);
                }
            }
        }
        Collections.reverse(rows);
        ChartData chartData = new ChartData(Arrays.asList("category","ratio"),rows);
        System.out.println(chartData);
        res.put("chartData",chartData);
        res.put("totalPrice",totalPrice);

        return res;
    }


    @GetMapping("/toTableMonth")
    public @ResponseBody
    Map<String,Object> toTableMonth() throws Exception {

        Map<String,Object> res = new HashMap<>();

        List<RowData> rows = new ArrayList<>();//chart rows

        List<Category> categories = new ArrayList<>();
        categories = categoryService.queryAllCategory();
        for(int i=0;i<categories.size();i++){
            rows.add(new RowData(categories.get(i).getName(), 0));
        }

        List<TableVo> tableVos = orderService.queryOrderByMonth();//本月所有订单的书本分类的名字和书本数量
        String totalPrice = orderService.queryOrderTotalPriceByMonth();//本月订单总金额
        Integer totalNum = NumberUtil.getTotalBookNum(tableVos);//本月所有书本总数量
        BigDecimal pecent = new BigDecimal("0");
        for (int i = 0; i < tableVos.size(); i++)  {
            TableVo tableVo = tableVos.get(i);
            HashMap<String, Object> map = new HashMap<>();
            String myPercent = NumberUtil.myPercent(tableVo.getBookNum(), totalNum);//百分比
            float percent = Float.parseFloat(myPercent);
            for(int j=0;j<categories.size();j++){
                if(rows.get(j).getCategory().equals(tableVo.getName())){
                    rows.get(j).setRatio(percent);
                }
            }
        }
        Collections.reverse(rows);
        ChartData chartData = new ChartData(Arrays.asList("category","ratio"),rows);
        System.out.println(chartData);
        res.put("chartData",chartData);
        res.put("totalPrice",totalPrice);

        return res;
    }

}
